﻿using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
//#define DEBUG //这里定义条件
namespace TechPoint.Demo
{
    internal class MainProgramClass
    {
        [DllImport("User32.dll")]
        public static extern int MessageBox(int hParent, string Message, string Caption, int Type);

        private static void Main(string[] args)
        {
            DisplayRunningMessage();
            DisplayDebugMessage();

            MessageBox(0, "Hello", "Message", 0);

            #region CodeReview Demo
            Console.WriteLine("=============================== CodeReview Demo ==========================");
            var info = typeof (MyClass);
            var attr = Attribute.GetCustomAttribute(info, typeof (CodeReviewAttribute)) as CodeReviewAttribute;
            if (attr != null)
            {
                Console.WriteLine("代码检查人：{0}",attr.Reviewer);
                Console.WriteLine("检查时间：{0}", attr.Date);
                Console.WriteLine("注释：{0}", attr.Comment);
            }

            #endregion

            Console.ReadLine();
        }

        [Conditional("DEBUG")]
        private static void DisplayRunningMessage()
        {
            Console.WriteLine("开始运行Main子程序。当前时间是" + DateTime.Now);
        }

        [Conditional("DEBUG")]
        [Obsolete]
        private static void DisplayDebugMessage()
        {
            Console.WriteLine("开始Main子程序");
        }
    }

    #region AttributeUsage|AttributeTargets Demo
    // 该Attribute只对类有效. 
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassTargetAttribute : Attribute
    {
    }


    // 该Attribute只对方法有效. 
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodTargetAttribute : Attribute
    {
    }


    // 该Attribute只对构造器有效。
    [AttributeUsage(AttributeTargets.Constructor)]
    public class ConstructorTargetAttribute : Attribute
    {
    }


    // 该Attribute只对字段有效. 
    [AttributeUsage(AttributeTargets.Field)]
    public class FieldTargetAttribute : Attribute
    {
    }


    // 该Attribute对类或者方法有效（组合）. 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class ClassMethodTargetAttribute : Attribute
    {
    }


    // 该Attribute对所有的元素有效.
    [AttributeUsage(AttributeTargets.All)]
    public class AllTargetsAttribute : Attribute
    {
    }

    //上面定义的Attribute施加到程序元素上的用法
    [ClassTarget] //施加到类
    [ClassMethodTarget] //施加到类
    [AllTargets] //施加到类
    public class TestClassAttribute
    {
        [ConstructorTarget] //施加到构造器
        [AllTargets] //施加到构造器
        private TestClassAttribute()
        {
        }

        [MethodTarget] //施加到方法
        [ClassMethodTarget] //施加到方法
        [AllTargets] //施加到方法
        public void Method1()
        {
        }

        [FieldTarget] //施加到字段
        [AllTargets] //施加到字段
        public int myInt;
    }
    #endregion

    #region CodeReview Demo

    [AttributeUsage(AttributeTargets.Class)] //还记得上一节的内容吗？
    public class CodeReviewAttribute : System.Attribute //定义一个CodeReview的Attribute
    {
        private string reviewer;  //代码检查人
        private string date;      //检查日期
        private string comment;   //检查结果信息

        //参数构造器
        public CodeReviewAttribute(string reviewer, string date)
        {
            this.reviewer = reviewer;
            this.date = date;
        }

        public string Reviewer
        {
            get
            {
                return reviewer;
            }
        }

        public string Date
        {
            get
            {
                return date;
            }
        }

        public string Comment
        {
            get
            {
                return comment;
            }
            set
            {
                comment = value;
            }
        }
    }

    [CodeReview("Niwalker", "2003-7-9", Comment = "Jack的代码")]
    public class MyClass
    {
        //类的成员定义
    }

    #endregion
}
